/**
 * @Title: warning_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @date 2014年10月27日 下午4:43:38
 * @version V1.0
 */
;
(function(){
    'use strict';

    var app = angular.module('fwa.mainApp');
    app.controller('CancelDrugStoreReviewCtrl', ['$scope','CommunityService','UpdateEvaluationService','OrgApproveEvaluationService','DrugStoreReviewService','$http','$modal','$state','BaseURL','Modal','FwaTabsetService','$stateParams',
        function($scope,CommunityService,UpdateEvaluationService,OrgApproveEvaluationService,DrugStoreReviewService,$http,$modal,$state,baseUrl,Modal,fwaTabsetService,$stateParams){

    	
    	   //保存tabclick的 点击对象
    	   $scope.tabClickData={};
    	   $scope.audit={}; //保存审批的意见和审批的描述
    	
    	  $scope.approveSelect=[{code:'1',label:'同意'},{code:'0',label:'不同意'}];
    	
    		//页面
    		$scope.includeUrl = 'views/hcpms/evaluation/evaluationDrugStore/drugFinish/finishCheckTableDrugStore.html';

    		$scope.intgrityEvalTypeInfo={};
            //tab切换事件
            $scope.tabClick = function(data) {
            	$scope.intgrityEvalTypeInfo=data;
            	if(data){
            		 $scope.selectedTab = data;
            		 
            		 // 根据诚信考评表子表 的主建去加载对应的 审核意见
                     if(data.intgrityEvalChildId){
                    	 $scope.tabClickData=data;
                    	 loadEvaluationOpinionInfo(data.intgrityEvalChildId);
                     }
            		 
                     if(data.evaluRuleId){
                     	loadIntgrityEvalDetailInfo(data.evaluRuleId,data.intgrityEvalChildId);
                     }
                     
            	}
               
                
             }
            //初始化请求参数
            var evaluationCode = null;
            if($stateParams.evaluationCode){
            	evaluationCode = $stateParams.evaluationCode;
            }
            
            
            
            /**
             * 根据诚信考评字表的id加载对应的审核意见
             */
            $scope.integrityEvalReview={};
            
            function loadEvaluationOpinionInfo(evalChildId){
            	 var parameter={
            			 evalChildId:evalChildId
            			 };
             OrgApproveEvaluationService.loadEvaluationOpinionInfo(parameter).success(function(response){
                     if(response.flag){
                    	   //审核的意见
                    	  if(response.auditData){
                    		  var opinion=response.auditData.opinion;
                    		  $scope.integrityEvalReview = response.auditData;
                    		    if(opinion==='1'){
                    		    	 $scope.integrityEvalReview.opinionValue="同意";
                    		    }else if(opinion==='0'){
                    		    	$scope.integrityEvalReview.opinionValue="不同意";
                    		    }
                    	  }
                    	  //审批意见
                    	  if(response.approveData){
                    		  var opinion=response.approveData.opinion;
                    		  var approveDesc=response.approveData.opinionDesc;
                    		  if(opinion==='1'){
                    			  $scope.audit.approveOpinion="同意";
                 		    }else if(opinion==='0'){
                 		    	$scope.audit.approveOpinion="不同意";
                 		    }
                    		$scope.audit.approveDesc=approveDesc;
                    	  }
                    	 
                    	 
                    }
                 });
            	
            }
           
            /**
             * 初始化诚信单位考评详情
             */
            $scope.intgrityEvalData = {};
            function loadIntgrityEvalData() {
                var parameter={
                		evaluationCode:$stateParams.evaluationCode
                		};
                OrgApproveEvaluationService.loadIntgrityEvalInfo(parameter).success(function(response){
                   if(response.flag){
                       $scope.intgrityEvalData = response.data;
                    }
               });
            }
            /**
             * 初始化诚信考评类别详情
             */
            $scope.tabs = [];
            function loadIntgrityEvalTypeInfo() {
                var parameter = {evaluationCode:evaluationCode};
                OrgApproveEvaluationService.loadEvaluationFinishInfo(parameter).success(function(response){
                    if(response.flag){
                    	$scope.tabs = response.list;
                    	 //第一次默认选中第一个tab页
                        $scope.tabClick($scope.tabs[0]);
                    }
                });
            }
            
            var intgrityEvalMap = {};
            /**
             * 初始化诚信考评详情
             */
            $scope.intgrityEvalInfo = {};
            function loadIntgrityEvalDetailInfo(evaluRuleId,intgrityEvalChildId) {
                if (!intgrityEvalMap[evaluRuleId]) {
                	 $scope.loading=true;
                	 var parameter= {evaluRuleId:evaluRuleId,intgrityEvalChildId:intgrityEvalChildId};
                	OrgApproveEvaluationService.loadIntgrityEvalDetailInfo(parameter).success(function(response){
                		 $scope.loading=false;
                        if (response.flag) {
                            intgrityEvalMap[evaluRuleId] = response.result;
                            getTableDatas(intgrityEvalMap[evaluRuleId]);
                        }
                    });
                }
                else {
                	$scope.intgrityEvalInfo = intgrityEvalMap[evaluRuleId];
                	getTableDatas(intgrityEvalMap[evaluRuleId]);
                }
                
            }

            //json 数据重新组合
            function getTableDatas(datas) {
                if (datas) {
                    var tableDatas = [];
                    angular.forEach(datas,function(oneData,i) {
                        var oneLength = 0;
                        oneData.no = i + 1;
                        tableDatas.push(oneData);
                        angular.forEach(oneData.list,function(twoData,j) {
                            twoData.rowSpan = twoData.list.length;
                            if (j == 0) {
                                oneData.data = twoData;
                            }
                            else {
                                twoData.oneHide = true;
                                tableDatas.push(twoData);
                            }
                            if (twoData.list && twoData.list.length > 0 ) {
                                angular.forEach(twoData.list,function(data,k) {
                                    oneLength++;
                                    if (data.isConfirm == '0') {
                                        data.checked = true;
                                    }
                                    else {
                                        data.checked = true;
                                    }
                                    if (k == 0) {
                                        twoData.data = data;
                                    }
                                    else {
                                        data.oneHide = true;
                                        data.twoHide = true;
                                        tableDatas.push(data);
                                    }
                                });
                            }
                            else {
                                oneLength++;
                            }
                        });
                        oneData.rowSpan = oneLength;
                    });
                    $scope.tableData = tableDatas;
                }
            }

            /**
             * 返回
             */
            $scope.goBack = function() {
            	  $state.go('evaluationOrgListManager.evaluationOrgList');
            };
            /**
             * 诚信审核岗的撤销事件
             */
            $scope.cancel = function() {
            	var parameter={
                		evaluationCode:$stateParams.evaluationCode
                		};
            		$scope.loading = true;
            		DrugStoreReviewService.cancelOperationApprove(parameter).success(function(res){
					$scope.loading = false;
					Modal.alert(res.msg);
				}).error(function(e){
					$scope.loading = false;
					Modal.alert("error");
				});
            };
            
            loadIntgrityEvalData();
            loadIntgrityEvalTypeInfo();
            
        }]);
})();
